package com.sznaner.tool.util;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.provider.Settings;
import android.view.Window;
import android.view.WindowManager;

public class ScreenBrightnessUtil {

    /**
     * 获得当前屏幕亮度的模式
     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
     * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
     */
    public static int getScreenMode( ) {
        int screenMode = 0;
        try {
            screenMode = Settings.System.getInt(NRTool.context().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE);
        } catch (Exception localException) {

        }
        return screenMode;
    }

    /**
     * 获得当前屏幕亮度值 0--255
     */
    public static int getScreenBrightness( ) {
        int screenBrightness = 255;
        try {
            screenBrightness = Settings.System.getInt(NRTool.context().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
        } catch (Exception localException) {

        }
        return screenBrightness;
    }

    /**
     * 设置当前屏幕亮度的模式
     * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
     * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
     */
    public static void setScreenMode(int paramInt) {
        try {
            Settings.System.putInt(NRTool.context().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, paramInt);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    /**
     * 设置当前屏幕亮度值 0--255
     */
    public static void setSystemScreenBrightness( int paramInt) {
        try {

            //先 设置屏幕亮度模式 手动
            setScreenMode(0);

            Uri uri = Settings.System.getUriFor(Settings.System.SCREEN_BRIGHTNESS);
            Settings.System.putInt(NRTool.context().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, paramInt);
            NRTool.context().getContentResolver().notifyChange(uri, null);
        } catch (Exception localException) {
            localException.printStackTrace();
        }
    }

    /**
     * 设置当前的activity屏幕亮度值，并使之生效
     */
    public static void setCurActivityScreenBrightness(Activity activity, int paramInt) {
        Window localWindow = activity.getWindow();
        WindowManager.LayoutParams localLayoutParams = localWindow.getAttributes();
        float f = paramInt / 255.0F;
        localLayoutParams.screenBrightness = f;
        localWindow.setAttributes(localLayoutParams);
    }

}
